package br.com.portalecbahia;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import br.com.portalecbahia.connectionservices.SynchronizeNews;
import br.com.portalecbahia.connectionservices.SynchronizeNotification;
import br.com.portalecbahia.customize.Loader;
import br.com.portalecbahia.dao.NotificationDAO;
import br.com.portalecbahia.listner.NewsListeners;
import br.com.portalecbahia.listner.NotificationListeners;

import com.google.analytics.tracking.android.EasyTracker;
import com.google.android.gcm.GCMRegistrar;


public class SplashActivity extends Activity  implements NewsListeners, NotificationListeners{
	 static final String SENDER_ID="381786147764"; // change me!

	private TextView ellipsisTX;
	public Context context;
	
	
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
       
        super.setContentView(R.layout.splash);
        
        // Create the adView
        GCMRegistrar.checkDevice(this);
	    
	    if (BuildConfig.DEBUG) {
	      GCMRegistrar.checkManifest(this);
	    }
        ellipsisTX = (TextView) findViewById(R.id.ellipsisTX);
        context = this;
       
        EasyTracker.getInstance().setContext(this);

        Handler handler = new Handler(){
    		@Override
 	 		public void handleMessage(Message msg){
    			 if (msg!= null) {
    				 String returnValue=(String)msg.getData().getString("ellipsis");
    				 ellipsisTX.setText(returnValue);
    			 }

    		}
		};
		new Loader().loading(handler);
	    new SynchronizeNews(this).execute();

    }
    
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    	
		boolean enable = new NotificationDAO(this).findStatusNotification();

    	final String regId=GCMRegistrar.getRegistrationId(this);

 	    if (regId.length() == 0) {
 	      GCMRegistrar.register(this, SENDER_ID);
 	    }
 	    else {
 	      Log.d(getClass().getSimpleName(), "Existing registration: "+ regId);
 	      GCMIntentService.id = regId;
 		  if(enable){
				 new SynchronizeNotification(this).execute(true);
 		  }else{
				 new SynchronizeNotification(this).execute(false);

 		  }

		}
    }
    
    @Override
    public void onStart() {
      super.onStart();
      EasyTracker.getInstance().activityStart(this); // Add this method.
    }

    @Override
    public void onStop() {
      super.onStop();
      EasyTracker.getInstance().activityStop(this); // Add this method.
    }
   
    @Override
	public void onSynchronizeNewsComplete (Boolean response) {
    	if(response==false){
			Toast.makeText(context, getString(R.string.internetOff), Toast.LENGTH_LONG).show();

    	}
    	Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        this.finish();
		
	}
    
    @Override
	public void onSynchronizeNotificationComplete(Boolean response) {
		
	}

}
